<template>
	<view>
		<view class="gl-container">
			<view class="gl-header">
				<view class="gl-header-left">
					<!--
					<uni-search-bar v-model="search" placeholder="关键字搜索" cancelButton="none"
						@confirm="getSearch"></uni-search-bar>
						-->
				</view>
				<view class="gl-header-right">
					<!--
					<view class="gl-button gl-button-success">
						<uni-icons type="folder-add" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">新增</text>
					</view>
					<view class="gl-button gl-button-warning">
						<uni-icons type="compose" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">编辑</text>
					</view>
					<view class="gl-button gl-button-error">
						<uni-icons type="trash" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">移除</text>
					</view>
					-->
					<view class="gl-button gl-button-base" hover-class="button-base-hover" :hover-stay-time="100"
						@click="refresh">
						<uni-icons type="reload" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">刷新</text>
					</view>
				</view>
			</view>
			<scroll-view scroll-y="true" class="scroll-Y">
				<uni-table stripe emptyText="暂无更多数据">
					<uni-tr>
						<uni-th align="center" width="30">ID</uni-th>
						<uni-th>昵称</uni-th>
						<uni-th>姓名</uni-th>
						<uni-th align="center">性别</uni-th>
						<uni-th align="center">超管</uni-th>
						<uni-th align="center">状态</uni-th>
						<uni-th>操作</uni-th>
					</uni-tr>
					<uni-tr v-for="(item, index) in tmpData" :key="index">
						<uni-td>{{item.id}}</uni-td>
						<uni-td>{{item.staff.nicename}}</uni-td>
						<uni-td>{{item.staff.basename}}</uni-td>
						<uni-td align="center">{{item.staff.gender?'男':'女'}}</uni-td>
						<uni-td align="center">
							<switch :checked="Boolean(item.is_super)" :disabled="true" style="transform:scale(0.7)" />
						</uni-td>
						<uni-td align="center">
							<switch :checked="Boolean(item.status)" :disabled="sConf.auth.admin.id === item.id"
								@change="status_admin(item);" style="transform:scale(0.7)" />
						</uni-td>
						<uni-td>
							<view class="gl-link gl-link-base" @tap="popAdminPermShow(item)">编辑权限</view>
						</uni-td>
					</uni-tr>
				</uni-table>
			</scroll-view>
			<view class="gl-footer">
				<uni-pagination :show-icon="true" :total="count" :pageSize="size" v-model="page" title="翻页"
					@change="hasTmpData" />
			</view>
		</view>
		
		<!-- 模态窗口 -->
		<pop-admin-perms ref="popAdminPerms" title='管理员权限' @logout="logout"></pop-admin-perms>
	</view>
</template>

<script>
	import utils from '@/common/utils.js'
	export default {
		name: "sub-admin-list",
		data() {
			return {
				sConf: getApp().globalData,
				count: null,
				size: 20,
				page: 1,
				search: '',
				tmpData: null
			};
		},
		mounted() {
			console.log('mounted');
			this.refresh();
		},
		methods: {
			logout() {
				this.$emit('logout');
			},
			refresh() {
				console.log('refresh');
				this.hasTmpData();
				// this.hasAuthPerms();
			},
			getSearch(e) {
				console.log('search', e);
			},
			hasTmpData() {
				let data = {
					page: this.page,
					size: this.size
				};
				utils.adminGetAdminList(this.sConf.host, this.sConf.auth.token, data).then((res) => {
					console.log('请求成功', res);
					if (res.rescode === 401) {
						this.logout();
					}
					if (res.rescode === 200) {
						this.tmpData = res.resmsg;
						this.count = res.count;
						this.size = res.size;
					}
				});
			},
			status_admin(e) {
				console.log('修改状态', e);
				let data = {
					'status': e.status ? 0 : 1
				}
				utils.adminPatchAdminList(this.sConf.host, this.sConf.auth.token, data, e.id).then((res) => {
					console.log('请求成功', res);
					if (res.rescode === 401) {
						this.logout();
					}
					if (res.rescode === 200) {}
				});
			},
			popAdminPermShow(e){
				console.log('管理员权限');
				this.$refs.popAdminPerms.open(e);
			}
		}
	}
</script>

<style lang="scss">

</style>